#include "Util.h"
#include <filesystem>
#include <SEngine/Project/Project.h>

namespace SEngine
{

    std::filesystem::path RelativePathAssistant(std::filesystem::path current, const std::filesystem::path & root) {
        if (!current.has_parent_path()) {
            throw std::runtime_error(root.string() + " is not in " + current.string());
        }
        if (current.filename().string() == ".." || current.filename().string() == ".") {
            throw std::runtime_error("Invaild Path");
        }
        if (current.parent_path() == root) {
            return current.filename();
        }
        return RelativePathAssistant(current.parent_path(), root) / current.filename();
    } 

    std::string AbsolutePath(const std::string &path)
    {
        if (path.empty()) {
            return "";
        }
        std::filesystem::path root(Project::s_root);  
        std::string out = (root / path).string();
        RelativePathAssistant(out, root); // 验证是否是引擎路径，否则抛出异常
        return out;
    }

    std::string RelativePath(const std::string &path)
    {
        std::filesystem::path root(Project::s_root);
        std::filesystem::path p(path);
        return RelativePathAssistant(p, root).string();
    }
} // namespace SEngine
